package com.dai.zhenfujin.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.bean.TypeListBean;
import com.dai.zhenfujin.view.RoundImageView;

import java.util.List;

public class VisitingTypeAdpter extends BaseAdapter {
    private Context context;
    private List<TypeListBean.TypelistBean> beanList;
    private LayoutInflater inflater;
    private int selectedPosition=0;


    public VisitingTypeAdpter(Context context, List<TypeListBean.TypelistBean> beanList) {
        this.context = context;
        this.beanList = beanList;
        inflater = LayoutInflater.from(context);
    }

    //这句是把listview的点击position,传递过来
    public void clearSelection(int position) {
        selectedPosition = position;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(com.dai.zhenfujin.R.layout.itme_type_list,null);
            holder.tvTypeName = (TextView)convertView.findViewById(com.dai.zhenfujin.R.id.tv_type_name);
            holder.ivType = (RoundImageView)convertView.findViewById(com.dai.zhenfujin.R.id.iv_type);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        TypeListBean.TypelistBean bean = beanList.get(position);
        holder.tvTypeName.setText(bean.getT_name());
        if (!TextUtils.isEmpty(bean.getT_url())){
            Glide.with(context).load(bean.getT_url()).into(holder.ivType);
        }else {
            holder.ivType.setVisibility(View.GONE);
        }

          //判断点击了哪个item,然后判断，让他的textview变色
        if (TextUtils.isEmpty(bean.getT_url())) {
            if (selectedPosition == position) {
                holder.tvTypeName.setTextColor(Color.parseColor("#fffec620"));
            } else {
                holder.tvTypeName.setTextColor(Color.parseColor("#404040"));
            }
        }
        return convertView;
    }

    class ViewHolder {
        private TextView tvTypeName;
        private RoundImageView ivType;
    }
}
